/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 2010 huliqing, huliqing.cn@gmail.com
 *
 * This file is part of QFaces.
 * QFaces is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * QFaces is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with QFaces.  If not, see <http://www.gnu.org/licenses/>.
 *
 * 这个文件是QFaces的一部分。
 * 您可以单独使用或分发这个文件，但请不要移除这个头部声明信息.
 * QFaces是一个自由软件，您可以自由分发、修改其中的源代码或者重新发布它，
 * 新的任何修改后的重新发布版必须同样在遵守LGPL3或更后续的版本协议下发布.
 * 关于LGPL协议的细则请参考COPYING、COPYING.LESSER文件，
 * 您可以在QFaces的相关目录中获得LGPL协议的副本，
 * 如果没有找到，请连接到 http://www.gnu.org/licenses/ 查看并获取副本。
 *
 * - Author: Huliqing
 * - Contact: huliqing.cn@gmail.com
 * - License: GNU Lesser General Public License (LGPL)
 * - Blog and source code availability: http://www.huliqing.name/
 */

Q.DateFormat = {
}

Q.DateFormat.format = function(date, pattern) {
	var pattern = pattern;
	if (!pattern)
		pattern = "yyyy-MM-dd";
	
	// Year
	var year4 = date.getFullYear();
    var year2 = year4.toString().substring(2);
    
    // Month
    var month = date.getMonth() + 1;
    var month2 = month;
    if (month2.toString().length == 1) {
    	month2 = "0" + month2;
    }
    
    // Day of month
    var dayOfMonth  = date.getDate();
    var dayOfMonth2 = dayOfMonth;
    var dayOfMonthLength = dayOfMonth.toString().length;
    if(dayOfMonthLength == 1){
        dayOfMonth2 = "0" + dayOfMonth;	
    }
    
    // Hours
    var hours  = date.getHours();
    var hours2 = hours;
    var hoursLength = hours.toString().length;
    if(hoursLength == 1){
        hours2 = "0" + hours;	
    }
    
    // hours 12小时制
    var hHours = hours;
    var hHours2;
    if (hHours > 11) {
    	hHours -= 12;
    }
    hHours2 = hHours;
    if (hHours2.toString().length == 1) {
    	hHours2 = "0" + hHours2;
    }
    
    // Minutes
    var minutes = date.getMinutes();
    var minutes2 = minutes;
    var minutesLength = minutes.toString().length;
    if(minutesLength == 1){
        minutes2 = "0" + minutes;	
    }
    
    // Seconds
    var seconds = date.getSeconds();
    var seconds2 = seconds;
    var secondsLength = seconds.toString().length;
    if(secondsLength == 1){
        seconds2 = "0" + seconds;	
    }
    
    // milliSeconds
    var milliSeconds=date.getMilliseconds();
    
    // day,获取星期几？ 0-6代表星期日-星期六
    var day=date.getDay();
    
    pattern = pattern.replace(/yyyy/,	year4);
    pattern = pattern.replace(/yy/,		year2);
    pattern = pattern.replace(/MM/,		month2);
    pattern = pattern.replace(/M/,		month);
    pattern = pattern.replace(/dd/, 	dayOfMonth2);
    pattern = pattern.replace(/d/, 		dayOfMonth);
    pattern = pattern.replace(/HH/, 	hours2);
    pattern = pattern.replace(/H/,		hours);
    pattern = pattern.replace(/hh/,		hHours2);
    pattern = pattern.replace(/h/,		hHours);
    pattern = pattern.replace(/mm/,		minutes2);
    pattern = pattern.replace(/m/,		minutes);
    pattern = pattern.replace(/ss/,		seconds2);
    pattern = pattern.replace(/s/,		seconds);
    pattern = pattern.replace(/S+/,		milliSeconds);
	return pattern;
}
Q.DateFormat.parse = function(str, pattern) {
	if (!str) return null;
	var y1 = pattern.indexOf("y");
	var y2 = pattern.lastIndexOf("y");
	var M1 = pattern.indexOf("M");
	var M2 = pattern.lastIndexOf("M");
	var d1 = pattern.indexOf("d");
	var d2 = pattern.lastIndexOf("d");
	var H1 = pattern.indexOf("H");
	var H2 = pattern.lastIndexOf("H");
	var h1 = pattern.indexOf("h");
	var h2 = pattern.lastIndexOf("h");
	var m1 = pattern.indexOf("m");
	var m2 = pattern.lastIndexOf("m");
	var s1 = pattern.indexOf("s");
	var s2 = pattern.lastIndexOf("s");
	var S1 = pattern.indexOf("S");
	var S2 = pattern.lastIndexOf("S");
	// 检查pattern格式是否正确
	if (y1 != -1 && y2 - y1 >= 4) return null; // No more than 4 "y"
	if (M1 != -1 && M2 - M1 >= 4) return null; // No more than 4 "M"
	if (d1 != -1 && d2 - d1 >= 2) return null; // No more than 2 "d"
	if (H1 != -1 && H2 - H1 >= 2) return null; // No more than 2 "H"
	if (h1 != -1 && h2 - h1 >= 2) return null; // No more than 2 "h"
	if (m1 != -1 && m2 - m1 >= 2) return null; // No more than 2 "m"
	if (s1 != -1 && s2 - s1 >= 2) return null; // No more than 2 "s"
	if (S1 != -1 && S2 - S1 >= 3) return null; // No more than 3 "S"
	
	var year, month, dayOfMonth, hours, minutes, seconds, milliSeconds;
	if (y1 != -1 && y2 < str.length) 
		year = parseInt(str.substring(y1, y2 + 1), 10);
	if (M1 != -1 && M2 < str.length)
		month = parseInt(str.substring(M1, M2 + 1), 10) - 1; // month from 0 to 11
	if (d1 != -1 && d2 < str.length)
		dayOfMonth = parseInt(str.substring(d1, d2 + 1), 10);
	if (H1 != -1 && H2 < str.length) // 优先使用24小时制
		hours = parseInt(str.substring(H1, H2 + 1), 10);
	if (h1 != -1 && h2 < str.length && !hours)
		hours = parseInt(str.substring(h1, h2 + 1), 10);
	if (m1 != -1 && m2 < str.length)
		minutes = parseInt(str.substring(m1, m2 + 1), 10);
	if (s1 != -1 && s2 < str.length)
		seconds = parseInt(str.substring(s1, s2 + 1), 10);
	if (S1 != -1 && S2 < str.length)
		milliSeconds = parseInt(str.substring(S1, S2 + 1), 10);
	
	if (month < 0 || month > 11 || dayOfMonth > 31 || hours > 24 || minutes > 59 || seconds > 59 || milliSeconds > 999)
		return null;
	
	if (!/\d+/.test(year)) return null; // 保证年份为数字
	
	var date = new Date();
	// 顺序不能颠倒
	if (milliSeconds != null) date.setMilliseconds(milliSeconds);
	if (seconds != null) date.setSeconds(seconds);
	if (minutes != null) date.setMinutes(minutes);
	if (hours != null) date.setHours(hours);
	if (dayOfMonth != null) date.setDate(dayOfMonth);
	if (month != null) date.setMonth(month);
	if (year != null) date.setYear(year);
//	alert("date=" + date.toLocaleDateString());
	return date;
	
}